/**
 * @author zhoushengtao
 * @since 2013-11-12 上午11:08:03
 */

package com.jason.android.core.weather;

import com.jason.android.core.http.AjaxCallBack;
import com.jason.android.core.http.HttpUtil;

import android.text.TextUtils;

public class Weather
{
    public interface WeatherCallBack
    {
        public void onSuccess(WeatherInfo info);

        public void onFailure(int error, String msg);
    }

    private static final String JSON_URL = "http://m.weather.com.cn/data/";
    private String mCityName = "";
    private WeatherInfo mWeatherInfo;

    public Weather(String cityName)
    {
        mCityName = TextUtils.isEmpty(cityName) ? "北京" : cityName.replaceAll("市","" );
    }

    public static String getCityWeatherUrl(String cityName)
    {
        return JSON_URL + CityId.getCityIdByName(cityName) + ".html";
    }

    public String getCityWeatherUrl()
    {
        return getCityWeatherUrl(mCityName);
    }

    public void getCityWeatherInfo(final WeatherCallBack callBack)
    {
        HttpUtil httpUtil = new HttpUtil();
        httpUtil.get(getCityWeatherUrl(), new AjaxCallBack<Object>()
        {
            @Override
            public void onLoading(long count, long current)
            {
            }

            public void onSuccess(Object t)
            {
                mWeatherInfo = new WeatherInfo();
                String weatherJson = t.toString();
                mWeatherInfo.loadData(weatherJson);
                if (callBack != null)
                    callBack.onSuccess(mWeatherInfo);
            }

            public void onFailure(Throwable t, int errorNo, String strMsg)
            {
                if (callBack != null)
                    callBack.onFailure(errorNo, strMsg);
            }
        });
    }
}
